﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NuMvvm;
using NuMvvm.Services;
using System.Windows.Input;
using NuMvvm.Commands;
using System.Collections.ObjectModel;
using Todo.Models;
using Todo.Services;
using Todo.Views;

namespace Todo.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        private DelegateCommand<int> _showItemCommand;

        public MainViewModel(
            IDataService dataService,
            IDialogService dialogService,
            INavigationService navigationService)
        {
            DataService = dataService;
            DialogService = dialogService;
            NavigationService = navigationService;

            Items = new ObservableCollection<TodoItem>();
        }

        public string Title
        {
            get
            {
                return "Main view";
            }
        }

        public ObservableCollection<TodoItem> Items { get; private set; }

        public async Task Initialize()
        {
            Items.Clear();
            foreach(var item in await DataService.GetItemsAsync())
            {
                Items.Add(item);
            }
        }

        public ICommand ShowItemCommand
        {
            get
            {
                return _showItemCommand ?? (_showItemCommand = DelegateCommand.Create<int>((id) =>
                {
                    NavigationService.Navigate(ViewKeys.Item, id);
                }));
            }
        }

        public IDataService DataService { get; private set; }

        public IDialogService DialogService { get; private set; }

        public INavigationService NavigationService { get; private set; }
    }
}
